﻿using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

public class UIBase
{
    public GameObject mGo;
    public RectTransform mTran;
    public UIHierarchy mHierarchy;

    public void Init(GameObject go)
    {
        mGo = go;
        mTran = mGo.transform as RectTransform;
        mHierarchy = mGo.GetComponent<UIHierarchy>();
        ExportHierarchy();
    }

    /// <summary>
    /// 导出元素到对应属性
    /// </summary>
    public void ExportHierarchy()
    {
        List<UIHierarchy.ItemInfo> widgets = mHierarchy.widgets;
        Type mType = GetType();
        for (int i = 0, len = widgets.Count; i < len; i++)
        {
            UIHierarchy.ItemInfo widget = widgets[i];
            FieldInfo info = mType.GetField(widget.name);
            if (info != null)
            {
                info.SetValue(this, widget.item);
            }
        }
    }

    public void Clear()
    {
        TimeManager.Instance.Remove(this);//关闭计时器
        MessageManager.Instance.RemoveListener(this);//关闭消息监听
    }
}